/*
 * @Author: cl
 * @Description:
 * @Date: 2023-04-04 15:07:29
 * @LastEditTime: 2023-05-05 17:12:51
 * @FilePath: \jsnode\nestjs\naota\src\main.ts
 * @LastEditors: cl
 */
import { NestApplication, NestFactory, Reflector } from '@nestjs/core'
import { AppModule } from './app.module'
import { VersioningType } from '@nestjs/common'
import * as session from 'express-session'
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'
import { Request, Response, NextFunction } from 'express'
import * as cors from 'cors'
import { Response as MyResponse } from './common/response'
import { Filter as MyFilter } from './common/Filter'
import { ValidationPipe } from '@nestjs/common'
import { TigerGuard } from './tiger/tiger.guard'

//白名单 | token 鉴权
const whiteList = ['/tiger/findAll']

//全局中间件
function MainMiddleWare(req: Request, res: Response, next: NextFunction) {
  // if(whiteList.includes(req.originalUrl)){
  //   res.send(`白名单大爷你先走`)
  //   next()
  // }else{
  //   console.log(`全局中间件:要想过此路留下买路财---`+req.originalUrl);
  //   res.send({msg:`全局中间件:要想过此路留下买路财`})
  //   next()
  // }
  next()
}

async function bootstrap() {
  //创建app
  const app = await NestFactory.create<NestApplication>(AppModule)
  app.useStaticAssets('uploads',{prefix:'/uploads'})
  //处理跨域
  app.use(cors())
  //增加统一的前缀
  app.setGlobalPrefix('api')
  //全局中间件
  // app.use(MainMiddleWare);
  // //注册网络请求拦截器
  app.useGlobalInterceptors(new MyResponse())
  // //注册异常拦截器
  // app.useGlobalFilters(new MyFilter());
  // //注册管道
  // app.useGlobalPipes(new ValidationPipe());
  // //注册守卫
  // app.useGlobalGuards(new TigerGuard(new Reflector()));
  //版本控制
  app.enableVersioning({
    type: VersioningType.URI,
  })
  //设置session
  // app.use(session())
  app.use(
    session({
      secret: 'xiaoli',
      name: 'xl.session',
      rolling: true,
      cookie: { maxAge: null },
    }),
  )

  //设置文档信息swaagger
  const options = new DocumentBuilder()
    .setTitle('测试接口文档')
    .setDescription('这是我自己搞得一个系统')
    .setVersion('1')
    .build()
  const document = SwaggerModule.createDocument(app, options)
  SwaggerModule.setup('/apidoc', app, document)

  await app.listen(4000)
}
bootstrap()
